package com.wgke.bluetooth.view;

import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class HorizontalStaggeredGridLayoutManager extends RecyclerView.LayoutManager {
    private int mSpanCount;
    private int mItemWidth;
    private int mTotalWidth;
    private List<Integer> mColumnHeights; // 记录每列当前的高度

    public HorizontalStaggeredGridLayoutManager(int spanCount) {
        this.mSpanCount = spanCount;
        this.mColumnHeights = new ArrayList<>(spanCount);
        for (int i = 0; i < spanCount; i++) {
            mColumnHeights.add(0);
        }
    }

    @Override
    public RecyclerView.LayoutParams generateDefaultLayoutParams() {
        return new RecyclerView.LayoutParams(
                RecyclerView.LayoutParams.WRAP_CONTENT,
                RecyclerView.LayoutParams.WRAP_CONTENT);
    }

    @Override
    public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
        if (getItemCount() == 0) {
            detachAndScrapAttachedViews(recycler);
            return;
        }

        if (getChildCount() == 0 && state.isPreLayout()) {
            return;
        }

        detachAndScrapAttachedViews(recycler);

        // 初始化列高度
        for (int i = 0; i < mSpanCount; i++) {
            mColumnHeights.set(i, getPaddingTop());
        }

        // 计算每个item的宽度
        mItemWidth = (getWidth() - getPaddingLeft() - getPaddingRight()) / mSpanCount;

        // 布局所有item
        for (int i = 0; i < getItemCount(); i++) {
            View view = recycler.getViewForPosition(i);
            addView(view);

            // 测量item
            measureChildWithMargins(view, 0, 0);

            // 找到当前最短的列
            int shortestColumn = findShortestColumn();
            int left = getPaddingLeft() + shortestColumn * mItemWidth;
            int top = mColumnHeights.get(shortestColumn);
            int right = left + mItemWidth;
            int bottom = top + getDecoratedMeasuredHeight(view);

            // 布局item
            layoutDecorated(view, left, top, right, bottom);

            // 更新列高度
            mColumnHeights.set(shortestColumn, bottom);
        }

        // 计算总高度
        mTotalWidth = findMaxColumnHeight();
    }

    private int findShortestColumn() {
        int shortestColumn = 0;
        int shortestHeight = mColumnHeights.get(0);
        for (int i = 1; i < mSpanCount; i++) {
            if (mColumnHeights.get(i) < shortestHeight) {
                shortestHeight = mColumnHeights.get(i);
                shortestColumn = i;
            }
        }
        return shortestColumn;
    }

    private int findMaxColumnHeight() {
        int maxHeight = 0;
        for (Integer height : mColumnHeights) {
            if (height > maxHeight) {
                maxHeight = height;
            }
        }
        return maxHeight;
    }

    @Override
    public boolean canScrollHorizontally() {
        return true;
    }

    @Override
    public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler, RecyclerView.State state) {
        // 实现水平滚动逻辑
        return dx;
    }

    @Override
    public int getWidth() {
        return super.getWidth();
    }
}